package cherry.data.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @Author : ms-wang
 * @Version : 1.0.0
 * @ClassName BeanUtil
 * @Description : 手动获取实例对象
 * @Date: 2022/1/14/014 10:50
 */
@Slf4j
@Component
public class BeanUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContextParam) throws BeansException {
        applicationContext = applicationContextParam;
    }

    /**
     * @ClassName BeanUtil.java
     * @author 谢宇翔
     * @version 1.0.0
     * @Description 手动获取bean
     * @createTime 2022年01月14日 20:31:00
     */
    public static Object getBean(Class<?> clazz) {
        log.info("获取接口实现：" + clazz);
        if (ObjectUtils.isEmpty(clazz)) {
            throw new NullPointerException("接口不能为空");
        }
        Object bean = applicationContext.getBean(clazz);
        if (ObjectUtils.isEmpty(bean)) {
            throw new NullPointerException("暂无接口实例");
        }
        return bean;
    }
}
